package com.c.ctgproject.ui.service

import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.ServiceOrderAdapter
import com.c.ctgproject.adapter.service.CommonSpannerAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.service.ServiceCommonOrderEntity
import com.c.ctgproject.utils.*
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.activity_common_order.*
import kotlinx.android.synthetic.main.activity_doorkeeper_part.mCarNumberET
import kotlinx.android.synthetic.main.activity_doorkeeper_part.mCleanIV
import kotlinx.android.synthetic.main.activity_doorkeeper_part.mSellerSpinner

/**
 *作者:LiBW
 *创建日期: 2021/6/29
 *描述: 客服订单管理 页面 (列表)
 */
class ServiceOrderListActivity : BaseActivity() {

    override fun getLayout() = R.layout.activity_common_order

    private val mViewModel by lazy { ViewModelProvider(this)[ServiceOrderListViewModel::class.java] }

    //订单来源
    private val orderFromList = ArrayList<String>()
    private val orderFromAdapter by lazy { CommonSpannerAdapter(this, orderFromList) }
    val title by lazy { intent.getStringExtra(MyConstants.TITLE_NAME).handlerNull() }
    private var search: String? = null

    //订单状态
    private var statue: String? = null

    //订单来源
    private var orderSource: String? = null
    private var page = 1

    private val mList = ArrayList<ServiceCommonOrderEntity.DataBean.ListBean>()
    private lateinit var mAdapter: ServiceOrderAdapter

    //获取数据来源 1.总订单相关(不含修改) 2.预订单管理 3.意向订单管理
    private var dataType = 1

    private fun handlerOrderStatue() {
        when (title) {
            "总订单管理" -> {
                //总订单管理包含 订单状态选择器
                mTabLayout.visibility = View.VISIBLE
                statue = null
                mAdapter = ServiceOrderAdapter(this, mList)
            }
            "预订单管理" -> {
                dataType = 2
                mAdapter = ServiceOrderAdapter(this, mList, true,dataType)
            }
            "意向订单管理" -> {
                dataType = 3
                mAdapter = ServiceOrderAdapter(this, mList, true,dataType)

            }
            "已取消订单" -> {
                statue = "0"
                mAdapter = ServiceOrderAdapter(this, mList)
            }
            "已完成订单" -> {
                statue = "3"
                mAdapter = ServiceOrderAdapter(this, mList,statue = "3")
            }
            "回退订单" -> {
                statue = "5"
                mAdapter = ServiceOrderAdapter(this, mList)
            }
            "待自送订单" -> {
                statue = "6"
                mAdapter = ServiceOrderAdapter(this, mList)
            }
            "物流管理" -> {
                //其他页面
            }
        }

    }

    fun getData(newData: Boolean = false) {
        MyLog.d("进来了么12345")
        loading.show()
        if (newData) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
            page = 1
        }
        when (dataType) {
            1 -> mViewModel.getAllOrder(search, statue, orderSource, page)
            2 -> mViewModel.servicePreOrder(search, orderSource, page)
            3 -> mViewModel.serviceTalkOrder(search, orderSource, page)
        }
    }

    override fun onResume() {
        super.onResume()
        MyLog.d("11有点意思")
        getData(true)
    }


    override fun event() {
        //处理刚进入APP时的订单来源
        handlerOrderStatue()
        mTitleBar.setTitle(title)
        //添加销售第一条信息
        orderFromList.add("订单来源")
        orderFromList.add("微信")
        orderFromList.add("400电话")
        orderFromList.add("车主自送")
        orderFromList.add("业务端")
        mSellerSpinner.adapter = orderFromAdapter
        //处理列表
        mOrderRV.adapter = mAdapter
        mOrderRV.layoutManager = LinearLayoutManager(this)

        mViewModel.error.observe(this, {
            mRefresh.over()
            loading.dismiss()
            MyLog.d("错误:$it")
            showToast(it)
        })

        mViewModel.result.observe(this, {
            mRefresh.over()
            loading.dismiss()
            MyLog.d("订单回调:$it")
            handlerHttpResult(it) {
                val data = getGson().fromJson(it, ServiceCommonOrderEntity::class.java).data
                data?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            }
        })

    }

    override fun click() {
        //搜索
        mSearchTV.setOnClickListener {
            val str = mCarNumberET.text.handlerNull()
            if (str.isNotEmpty()) {
                search = str
                MyLog.d("22有点意思")
                getData(true)
            }
        }

        //清空输入框
        mCleanIV.setOnClickListener {
            mCarNumberET.textSet("")
        }
        mCarNumberET.handlerText {
            if (it.isEmpty()) {
                search = ""
                getData(true)
            }
        }
        var canSearch=false
        //订单来源选择
        mSellerSpinner.handlerSelect {
            MyLog.d("选择了${orderFromList[it]}")
            when (orderFromList[it]) {
                "订单来源" -> {
                    orderSource=null
                }
                "微信" -> {
                    orderSource="1"
                }
                "400电话" -> {
                    orderSource="2"
                }
                "车主自送" -> {
                    orderSource="3"
                }
                "业务端" -> {
                    orderSource="4"
                }
            }
            MyLog.d("44有点意思")
            if (canSearch){
                getData(true)
            }else{
                canSearch=true
            }
        }
        mRefresh.setOnLoadMoreListener {
            page++
            MyLog.d("55有点意思")
            getData()
        }

        mRefresh.setOnRefreshListener {
            page = 1
            mList.clear()
            MyLog.d("66有点意思")
            getData()
        }
        mTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                statue = when (tab?.text.handlerNull()) {
                    "全部" -> null
                    "取消" -> "0"
                    "待处理" -> "1"
                    "待物流处理" -> "2"
                    "已完成" -> "3"
                    "待沟通" -> "4"
                    "回退订单" -> "5"
                    "待自送" -> "6"
                    else -> null
                }
                MyLog.d("77有点意思")
                getData(true)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })
    }
}